package cn.etgps.etgps.blmanager;

import android.content.Context;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.etgps.etgps.dataEntity.ChangePwdEntity;
import cn.etgps.etgps.dataEntity.LoginBean;
import cn.etgps.etgps.manager.NetworkManager;
import cn.etgps.etgps.utils.AppUtils;
import cn.etgps.etgps.utils.Constant;

/**
 * 提供关于用户的功能
 * Created by Nicky on 2015/12/2.
 */
public class UserManager extends BaseBLManager {
    public UserManager(Context context) {
        super(context);
    }

    /**
     * 用户登录
     *
     * @param info      用户登录信息，账号密码
     * @param loginBean 获取登录后的数据
     * @return 获取成果，成功/失败/无法连接服务器
     */
    public synchronized String login(String[] info, LoginBean loginBean) {
        String result = NetworkManager.getDataFromServer(buildLoginPath(info));
        if (result.equals(Constant.CONNECT_TO_SERVER_FAILED)) {
            return Constant.CONNECT_TO_SERVER_FAILED;
        } else {
            LoginBean entity = JSON.parseObject(result.replaceAll("\\[", "").replaceAll("\\]", ""), LoginBean.class);
            if (entity != null) {
                if (!(entity.getUser_id()).equals("-1")) {   //等于-1表示登录不成功
                    loginBean.setUser_id(entity.getUser_id());
                    loginBean.setUser_type(entity.getUser_type());
                    return Constant.RETURN_RESULT_OK; //登录成功返回ok
                } else {
                    return Constant.CONNECT_TO_SERVER_FAILED; //登录失败
                }
            } else {
                return Constant.CONNECT_TO_SERVER_FAILED;
            }
        }
    }

    /**
     * 构造用户登录URL
     *
     * @param info 用户登录信息
     * @return 登陆需要的URL
     */
    private String buildLoginPath(String[] info) {
        return Constant.URL_GET_TRUCK_LOGIN + Constant.URL_LOGIN_USER_NAME_KEY + AppUtils.doURLEncode(info[0])
                + Constant.URL_LOGIN_USER_PASSWORD_KEY + AppUtils.doURLEncode(info[1]);
    }

    /**
     * 发送用户反馈
     *
     * @param comment 评论内容
     * @return 获取成果，成功/失败/无法连接服务器
     */
    public synchronized String saveFeedback(String comment) {
        String userName = getUserInfo()[0];
        String userType = getUserType();
        Map<String, String> params = new HashMap<>();
        params.put(Constant.SAVE_FEEDBACK_UID, userName);
        params.put(Constant.SAVE_FEEDBACK_COMMENT, comment);
        params.put(Constant.SAVE_FEEDBACK_USERTYPE, userType);
        String result = NetworkManager.postDataToServer(Constant.URL_SAVE_FEEDBACK, params);
        if (Constant.CONNECT_TO_SERVER_FAILED.equals(result)) {
            return Constant.CONNECT_TO_SERVER_FAILED;
        } else {
            try {
                JSONObject json = new JSONObject(result);
                String isSucceed = json.getString(Constant.SAVE_FEEDBACK_STATUS);
                if (isSucceed.equals(Constant.RETURN_RESULT_OK)) {//OK返回OK
                    return isSucceed;
                } else {
                    return json.getString(Constant.SAVE_FEEDBACK_ERR_MESSAGE);//Failed返回错误信息
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return Constant.CONNECT_TO_SERVER_FAILED;
        }
    }

    /**
     * 修改密码
     *
     * @param info 旧密码，新密码等信息
     * @return 修改结果
     */
    public synchronized String changePwd(String[] info) {
        String result = NetworkManager.getDataFromServer(changePwdBuildPath(info));
        if (result.equals(Constant.CONNECT_TO_SERVER_FAILED)) {
            return Constant.CONNECT_TO_SERVER_FAILED;
        } else {
            ChangePwdEntity entity = JSON.parseObject(result, ChangePwdEntity.class);
            if (entity.getStatus().equals(Constant.RETURN_RESULT_OK)) {
                return Constant.RETURN_RESULT_OK;
            } else {
                return entity.getErrorMsg();
            }
        }
    }

    /**
     * 构造修改密码的URL
     */
    private String changePwdBuildPath(String[] info) {
        return Constant.URL_CHANGE_PWD +
                Constant.CHANGE_PWD_UNAME_KEY +
                AppUtils.doURLEncode(info[0]) +
                Constant.CHANGE_PWD_OLD_PWD_KEY +
                AppUtils.doURLEncode(info[1]) +
                Constant.CHANGE_PWD_NEW_PWD_KEY +
                AppUtils.doURLEncode(info[2]) +
                Constant.CHANGE_PWD_USER_TYPE_KEY +
                AppUtils.doURLEncode(info[3]);
    }
}
